##Replication code for Post, Alison E. and Maria Victoria Murillo. 2015. "How Investor Portfolios Shape Regulatory Outcomes: Privatized Infrastructure After Crises." World Development.  

##Code for Cox survival analysis of contract renegotiation presented in article and on-line appendix##

library(foreign)
library(survival)
library(simPH)

	##Load data##
renegdata <- read.csv("investorrenegotiation.csv", header=TRUE)

##Note that we have two versions of each IV for investor characteristics and investor name in a given year. This is necessary becuase in a few cases, an investor exited and a new one came in the same year and concluded a contract renegotiation within that same year. We therefore created two versions of the variable for a given year, one corresponding to exit decisions and one to renegotiation decisions.##   

	##Create composite variables## 
renegdata$Investor_Type_Ren3 <- relevel(renegdata$Investor_Type_Ren2,"LDC") #variable leveled so LDC the baseline category#
renegdata$InvestorLME <- renegdata$Investor_Type_Ren=="LME"
renegdata$InvestorCME <- renegdata$Investor_Type_Ren=="CME"
renegdata$Rep_Exit_Cost_Addmain_Ren <- renegdata$Large_AR_Port_Ren + renegdata$Sector_Brand_Ren + renegdata$Other_Reg_Assets_Ren #main version of reputational exit cost index#
renegdata$Rep_Exit_Cost_Reweighted_Ren <- renegdata$Large_AR_Port_Ren + renegdata$Sector_Brand_Ren + 2*(renegdata$Other_Reg_Assets_Ren) #version of reputational exit cost index that weights domestic audiences as heavily as international ones#

precrisis.entry <- renegdata[renegdata$PreCrisis_Entry_Ren==1,]
postcrisis.entry <- renegdata[renegdata$PreCrisis_Entry_Ren==0,]
wat <- renegdata[renegdata$Sector=="Water",]
elec <- renegdata[renegdata$Sector=="Electricity",]

##TABLES IN THE ARTICLE##

##Table 4. Cox Proportional Hazard Analysis of Contract Renegotiation (Full Accords) for Electricity and W&S, 2003-2009##
USING PARALLEL SPECIFICATIONS TO EXIT MODELS IN TABLE 3 (WITHOUT PROPORTIONALITY CORRECTIONS, SO AS TO KEEP SPECIFICATIONS PARALLEL)

	#Model 1#  
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)

 	#dfbetas for key variables#
	par(mfrow=c(2,2))
	rr <- resid(cfit, type='dfbeta')
	plot(renegdata$Diversified_Ren[1:188], rr[,1], xlab="Diversified", ylab="Influence for Diversification Variable")  
	 identify(renegdata$Diversified_Ren[1:188], rr[,1])  #last seven obs ommitted because of NAs#
	 
	 par(mfrow=c(2,2))
	rr <- resid(cfit, type='dfbeta')
	plot(renegdata$Rep_Exit_Cost_Reweighted_Ren[1:188], rr[,2], xlab="Rep", ylab="Influence for Rep Variable")  
	 identify(renegdata$Rep_Exit_Cost_Reweighted_Ren[1:188], rr[,2])  #last seven obs ommitted because of NAs#


#quantities of interest#

cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)

sim1 <- coxsimLinear(cfit,b="Diversified_Ren", qi="First Difference", Xj=1, Xl=0)
mean(sim1$QI)
sd(sim1$QI)

sim1 <- coxsimLinear(cfit, b="Diversified_Ren", Xj=1, Xl=0)
mean(sim1$QI)
sd(sim1$QI)

sim1 <- coxsimLinear(cfit, b="Diversified_Ren", qi="Hazard Ratio", Xj=1, Xl=0)
mean(sim1$QI)
sd(sim1$QI)

sim1 <- coxsimLinear(cfit, b="Diversified_Ren", qi="Relative Hazard", Xj=1, Xl=0)
mean(sim1$QI)
sd(sim1$QI)

#Model 2# 
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Sector_Brand_Ren + Large_AR_Port_Ren + Other_Reg_Assets_Ren + Fin_Exit_Cost_Ren + Private_Ren + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)

#Model 3# 
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + Investor_Type_Ren3 + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)

#Model 4# 
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + Domestic_Ren + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)

#Model 5# 
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + PBG_1999 + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)

#Model 6# 
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren +  log(HH_2001) + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)

#Model 7# 
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + Formal_Independence + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)

#Model 8# 
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + encpdn + Gov_Aligned2  + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)


##CODE FOR TABLES FROM ONLINE APPENDIX##

##Table A.IV. Cox Proportional Hazard Regressions of Contract Renegotiation (Partial Accords) For Electricity and W&S Contracts, 2003-2009##

renegdata <- read.csv("investorreneg_partial.csv", header=TRUE)
renegdata$Investor_Type_Ren3 <- relevel(renegdata$Investor_Type_Ren2,"LDC")
renegdata$InvestorLME <- renegdata$Investor_Type_Ren=="LME"
renegdata$InvestorCME <- renegdata$Investor_Type_Ren=="CME"
renegdata$Rep_Exit_Cost_Addmain_Ren <- renegdata$Large_AR_Port_Ren + renegdata$Sector_Brand_Ren + renegdata$Other_Reg_Assets_Ren
renegdata$Rep_Exit_Cost_Reweighted_Ren <- renegdata$Large_AR_Port_Ren + renegdata$Sector_Brand_Ren + 2*(renegdata$Other_Reg_Assets_Ren)

#Model 1#  
cfit <- coxph(Surv(Start,Stop,Par_Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)
extractAIC(cfit)

#Model 2# 
cfit <- coxph(Surv(Start,Stop,Par_Reneg_In_Year) ~ Diversified_Ren + Sector_Brand_Ren + Large_AR_Port_Ren + Other_Reg_Assets_Ren + Fin_Exit_Cost_Ren + Private_Ren + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)
extractAIC(cfit)

#Model 3# 
cfit <- coxph(Surv(Start,Stop,Par_Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + Investor_Type_Ren3 + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)  
extractAIC(cfit)

#Model 4# 
cfit <- coxph(Surv(Start,Stop,Par_Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + Domestic_Ren + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)
extractAIC(cfit)

#Model 5# 
cfit <- coxph(Surv(Start,Stop,Par_Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + PBG_1999 + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)
extractAIC(cfit)

#Model 6# 
cfit <- coxph(Surv(Start,Stop,Par_Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren +  log(HH_2001) + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)
extractAIC(cfit)

#Model 7# 
cfit <- coxph(Surv(Start,Stop,Par_Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + Formal_Independence + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)
extractAIC(cfit)

#Model 8# 
cfit <- coxph(Surv(Start,Stop,Par_Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + encpdn + Gov_Aligned2  + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)
extractAIC(cfit)


##Table A.VI. Cox Proportional Hazard Model of Contract Renegotiation (Full Accords) for Electricity and W&S, 2003-2003 (with Sector Dummies)
renegdata <- read.csv("investorrenegotiation.csv", header=TRUE)

	##Create composite variables## 
renegdata$Investor_Type_Ren3 <- relevel(renegdata$Investor_Type_Ren2,"LDC") #variable leveled so LDC the baseline category#
renegdata$InvestorLME <- renegdata$Investor_Type_Ren=="LME"
renegdata$InvestorCME <- renegdata$Investor_Type_Ren=="CME"
renegdata$Rep_Exit_Cost_Addmain_Ren <- renegdata$Large_AR_Port_Ren + renegdata$Sector_Brand_Ren + renegdata$Other_Reg_Assets_Ren #main version of reputational exit cost index#
renegdata$Rep_Exit_Cost_Reweighted_Ren <- renegdata$Large_AR_Port_Ren + renegdata$Sector_Brand_Ren + 2*(renegdata$Other_Reg_Assets_Ren) #version of reputational exit cost index that weights domestic audiences as heavily as international ones#

precrisis.entry <- renegdata[renegdata$PreCrisis_Entry_Ren==1,]
postcrisis.entry <- renegdata[renegdata$PreCrisis_Entry_Ren==0,]
wat <- renegdata[renegdata$Sector=="Water",]
elec <- renegdata[renegdata$Sector=="Electricity",]

	#Model 1#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + Sector + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)
extractAIC(cfit)

	#Model 2#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Sector_Brand_Ren + Large_AR_Port_Ren + Other_Reg_Assets_Ren + Fin_Exit_Cost_Ren + Private_Ren + Sector + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit) 
extractAIC(cfit)

	#Model 3#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + Investor_Type_Ren3 + Sector + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)
extractAIC(cfit)

	#Model 4#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + Domestic_Ren + Sector + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)
extractAIC(cfit)

	#Model 5#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + PBG_1999 + Sector + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)
extractAIC(cfit)

	#Model 6#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren +  log(HH_2001) + Sector + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)
extractAIC(cfit)

	#Model 7#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + Formal_Independence + Sector + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit)
extractAIC(cfit)

	#Model 8#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + encpdn + Gov_Aligned2  + Sector + cluster(Concession) + cluster(Province), weights=Weights, data=renegdata)
cox.zph(cfit)
summary(cfit) 
extractAIC(cfit)


##Table A.IX. Cox Proportional Hazard Analysis of Contract Renegotiation, 2003-2009 (Results by Sector) 

	#Model 1 for electricity#	
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren +  + cluster(Concession) + cluster(Province), weights=Weights, data=elec)
cox.zph(cfit)
summary(cfit)


	#Model 1 for water#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Private_Ren + Fin_Exit_Cost_Ren + cluster(Concession) + cluster(Province), weights=Weights, data=wat)
cox.zph(cfit)
summary(cfit)



##Table A.XIII: Cox Proportional Hazard Analysis of Contract Renegotiation for Electricity and W&S, 2003-2009 (Pre-Crisis Entrants)##

	##PRE-CRISIS ENTRANTS##
	#Model 1#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=precrisis.entry)
cox.zph(cfit)
summary(cfit)  

	#Model 2#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Sector_Brand_Ren + Large_AR_Port_Ren + Other_Reg_Assets_Ren + Fin_Exit_Cost_Ren + Private_Ren + cluster(Concession) + strata(Sector) + cluster(Province), weights=Weights, data=precrisis.entry)
cox.zph(cfit)
summary(cfit)  

	#Model 3#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + Investor_Type_Ren3 + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=precrisis.entry)
cox.zph(cfit)
summary(cfit) 

	#Model 4#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + Domestic_Ren + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=precrisis.entry)
cox.zph(cfit)
summary(cfit) 

#Model 5#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + PBG_1999  + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=precrisis.entry)
cox.zph(cfit)
summary(cfit)

#Model 6#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + log(HH_2001)  + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=precrisis.entry)
cox.zph(cfit)
summary(cfit)

#Model 7#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + Formal_Independence  + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=precrisis.entry)
cox.zph(cfit)
summary(cfit)

#Model 8#
cfit <- coxph(Surv(Start,Stop,Reneg_In_Year) ~ Diversified_Ren + Rep_Exit_Cost_Addmain_Ren + Fin_Exit_Cost_Ren + Private_Ren + Gov_Aligned2 + encpdn  + strata(Sector) + cluster(Concession) + cluster(Province), weights=Weights, data=precrisis.entry)
cox.zph(cfit)
summary(cfit)

